// mypage/cashOrder/cashOrder.js
var app=getApp();
var util=require("../../utils/util");
var my=require("../../api/my");


Page({
  /*页面的初始数据*/
  data: {
    id: '',
    all_check: false,
    total_money: "0.00",
    order_num: '',
    orders:[],
    currentPage: 1,
    pageSize: 10,
    hasMoreData: true
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this;
    that.setData({id:options.id})
    //
    that.get_cash_order_list(options.id);
  },
  // 单选
  check_item:function(e){
    var that = this,orders=that.data.orders;
    var index=e.currentTarget.dataset.index;
    if(orders[index].checked){
      orders[index].checked=false
    }else{
      orders[index].checked=true
    }
    that.setData({orders:orders})
    that.is_check(orders)
  },
  // 全选
  check_all:function(){
    var that = this,orders=that.data.orders,all_check=that.data.all_check;
    orders.forEach((item,i)=>{
      if(all_check){
        orders[i].checked=false
      }else{
        orders[i].checked=true
      }
    })
    that.setData({orders:orders,all_check: !all_check})
    that.is_check_all(orders)
  },
  // 全选金额
  is_check_all:function(obj){
    var that = this,money=0,order_num=[],all_check=that.data.all_check;
    if(all_check){
      for(var i=0;i<obj.length;i++){
        money +=Number(obj[i].money);
        order_num.push(obj[i].id)
      }
    }
    var text=order_num.join(",")
    that.setData({total_money: money.toFixed(2),order_num:text})
  },
  // 判断是否全选（单选）
  is_check:function(obj){
    var that = this,money=0,order_num=[],all_check=true;
    for(var i=0;i<obj.length;i++){
      if(obj[i].checked){
        money +=Number(obj[i].money);
        order_num.push(obj[i].id)
      }else{
        all_check=false;
      }
    }
    var text=order_num.join(",")
    that.setData({total_money: money.toFixed(2),order_num:text,all_check:all_check})
  },
  // 触底
  onReachBottom:function(){
    var that=this,hasMoreData=that.data.hasMoreData,id=that.data.id;
    if(hasMoreData){
      that.get_cash_order_list(id)
    }else{
      util.page_show_toast('没有更多了',1500)
    }
  },
  // 确定
  confirm_money:function(){
    var that=this,money=that.data.total_money,order_num=that.data.order_num;
    if(money<1){
      util.page_show_toast('单笔提现最低金额不能低于1元',3000)
      return false;
    }
    if(money>5000){
      util.page_show_toast('单笔提现最高金额不能超过5000元',3000)
      return false;
    }
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    prevPage.setData({cash_money:money>0?money:'',money2:money>=1000?util.toThousands(util.toDecimal(money)):util.toDecimal(money),text3:order_num})
    wx.navigateBack({
      delta: 1
    })
  },
  // 订单列表
  get_cash_order_list:function(id){
    wx.showLoading({title:"加载中"})
    var that=this,currentPage=that.data.currentPage,pageSize=that.data.pageSize;
    var params={
      "businessId": id,
      "currentPage": currentPage,
      "pageSize": pageSize
    }
    my.get_cash_order_list(params).then(res=>{
      wx.hideLoading()
      if(res.data.code==200){
        var list=res.data.data.records;
        for(var i=0;i<list.length;i++){
          list[i].paymentTime=util.formatTime(new Date(list[i].paymentTime))
          if(list[i].floorPrice&&list[i].oneAgent&&list[i].twoAgent){
            list[i].money=list[i].floorPrice
          }else{
            list[i].money=(list[i].paidInAmount+list[i].platformDiscountAmount)?util.toDecimal((list[i].paidInAmount+list[i].platformDiscountAmount)):0
          }
          list[i].checked=false
        }
        var orders = that.data.orders;
        orders = util.extend(orders, list)
        if (list.length < pageSize) {
          that.setData({
            orders: orders,
            hasMoreData: false
          })
        } else {
          that.setData({
            orders: orders,
            hasMoreData: true,
            currentPage: currentPage + 1
          })
        }
      }
    })
  },
})